package cn.highset.modules.base.controller.manage;

import cn.highset.common.utils.PageUtil;
import cn.highset.common.utils.ResultUtil;
import cn.highset.common.vo.PageVo;
import cn.highset.common.vo.Result;
import cn.highset.common.vo.SearchVo;
import cn.highset.modules.base.entity.MiniprogramPath;
import cn.highset.modules.base.factory.MiniprogramPathFactory;
import cn.highset.modules.base.service.MiniprogramPathService;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author Victor
 */
@Slf4j
@RestController
@Api(description = "小程序可选跳转页面表管理接口")
@RequestMapping("/highset/miniprogramPath")
@CacheConfig(cacheNames = "miniprogramPath")
@Transactional
public class MiniprogramPathController {

    @Autowired
    private MiniprogramPathService miniprogramPathService;


    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<MiniprogramPath>> getByCondition(@ModelAttribute MiniprogramPath miniprogramPath,
                                                        @ModelAttribute SearchVo searchVo,
                                                        @ModelAttribute PageVo pageVo) {

        Page<MiniprogramPath> page = miniprogramPathService.findByCondition(miniprogramPath, searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<Page<MiniprogramPath>>().setData(page);
    }

    @CacheEvict(key = "'all'")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加页面")
    public Result<Object> add(@ModelAttribute MiniprogramPath mp) {
        if (StrUtil.hasBlank(mp.getName(), mp.getPath())) {
            return new ResultUtil<Object>().setErrorMsg("缺少必需表单字段");
        }

        MiniprogramPath miniprogramPath = miniprogramPathService.save(mp);
        if (miniprogramPath == null) {
            return new ResultUtil<Object>().setErrorMsg("添加失败");
        }
        return new ResultUtil<Object>().setSuccessMsg("添加成功");
    }

    @CacheEvict(key = "'all'")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "修改页面", notes = "需要通过id获取原轮播图信息")
    public Result<Object> edit(@ModelAttribute MiniprogramPath mp) {
        if (StrUtil.hasBlank(mp.getId(), mp.getName(), mp.getPath())) {
            return new ResultUtil<Object>().setErrorMsg("缺少必需表单字段");
        }

        MiniprogramPath old = miniprogramPathService.get(mp.getId());

        MiniprogramPath miniprogramPath = miniprogramPathService.update(MiniprogramPathFactory.edit(mp, old));

        if (miniprogramPath == null) {
            return new ResultUtil<Object>().setErrorMsg("修改失败");
        }
        return new ResultUtil<Object>().setSuccessMsg("修改成功");
    }

    @CacheEvict(key = "'all'")
    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过ids删除", notes = "逻辑删除")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(paramType = "path", name = "ids", value = "页面ID数组", dataType = "String", required = true, allowMultiple = true),
    })
    public Result<Object> delAllByIds(@PathVariable String[] ids) {
        for (String id : ids) {
            miniprogramPathService.delete(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部页面")
    public Result<Object> getAll() {
        return new ResultUtil<Object>().setData(miniprogramPathService.getAllData());
    }

}
